package com.btpj.hospital_server.controller;

import com.btpj.hospital_server.dto.CommonPage;
import com.btpj.hospital_server.dto.CommonResult;
import com.btpj.hospital_server.dto.param.HospitalSpecialParam;
import com.btpj.hospital_server.entity.HospitalOutpatient;
import com.btpj.hospital_server.entity.HospitalSpecial;
import com.btpj.hospital_server.service.HospitalSpecialService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 医院专科Controller
 *
 * @author BTPJ  2020/8/27
 */
@Api(tags = "医院专科接口")
@RestController
@RequestMapping("/special")
public class HospitalSpecialController {

    @Resource
    HospitalSpecialService hospitalSpecialService;

    @ApiOperation("添加专科信息")
    @ApiImplicitParam(name = "name", value = "专科名称", required = true, dataType = "String", paramType = "query")
    @PostMapping("/add")
    public CommonResult<Void> addSpecial(HospitalSpecialParam param) {
        String name = param.getName();
        if (name == null || name.trim().isEmpty()) {
            return CommonResult.error("专科名不可为空");
        }
        if (hospitalSpecialService.existSpecial(name)) {
            return CommonResult.error("已存在此名称的专科");
        }
        if (hospitalSpecialService.addSpecial(param)) {
            return CommonResult.success("添加专科信息成功");
        }

        return CommonResult.error("服务器错误，添加专科信息失败");
    }

    @ApiOperation("修改专科信息")
    @ApiImplicitParam(name = "id", value = "专科编号", required = true, dataType = "Long", paramType = "path")
    @PutMapping("/update/{id}")
    public CommonResult<Void> updateSpecial(@PathVariable Long id, HospitalSpecialParam param) {
        if (!hospitalSpecialService.existSpecial(id)) {
            return CommonResult.error("不存在指定编号的专科");
        }

        String name = param.getName();
        if (name != null) {
            if (name.trim().isEmpty()) {
                return CommonResult.error("专科名不可为空");
            }
            if (hospitalSpecialService.existSpecial(name)) {
                return CommonResult.error("已存在此名称的专科");
            }
        }

        if (hospitalSpecialService.updateSpecial(id, param)) {
            return CommonResult.success("修改专科信息成功");
        }

        return CommonResult.error("服务器错误，修改专科信息失败");
    }

    @ApiOperation("删除专科信息")
    @ApiImplicitParam(name = "id", value = "专科编号", required = true, dataType = "Long", paramType = "path")
    @DeleteMapping("/delete/{id}")
    public CommonResult<Void> updateSpecial(@PathVariable Long id) {
        if (!hospitalSpecialService.existSpecial(id)) {
            return CommonResult.error("不存在指定编号的专科");
        }

        if (hospitalSpecialService.deleteSpecial(id)) {
            return CommonResult.success("删除专科信息成功");
        }

        return CommonResult.error("服务器错误，删除专科信息失败");
    }

    @ApiOperation("获取专科信息")
    @ApiImplicitParam(name = "id", value = "专科编号", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/detail/{id}")
    public CommonResult<HospitalSpecial> getSpecialDetails(@PathVariable Long id) {
        if (!hospitalSpecialService.existSpecial(id)) {
            return CommonResult.error("不存在指定编号的专科");
        }

        HospitalSpecial special = hospitalSpecialService.getSpecialDetails(id);
        if (special != null) {
            return CommonResult.success(special);
        }

        return CommonResult.error("服务器错误，获取专科信息失败");
    }

    @ApiOperation("获取专科分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "专科名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "第几页", required = true, paramType = "query", dataType = "Int"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, paramType = "query", dataType = "Int"),
    })
    @GetMapping("/pageList")
    public CommonResult<CommonPage<HospitalSpecial>> getSpecialPageList(@RequestParam(required = false) String name,
                                                                        @RequestParam Integer pageNo,
                                                                        @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        CommonPage<HospitalSpecial> commonPage = CommonPage.listPage(hospitalSpecialService.getList(name));
        return CommonResult.success(commonPage);
    }

    @ApiOperation("获取专科所属门诊分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "专科编号", required = true, paramType = "path", dataType = "Long"),
            @ApiImplicitParam(name = "pageNo", value = "第几页", required = true, paramType = "query", dataType = "Int"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, paramType = "query", dataType = "Int"),
    })
    @GetMapping("/outpatientPageList/{id}")
    public CommonResult<CommonPage<HospitalOutpatient>> getOutpatientPageListBySpecial(@PathVariable Long id,
                                                                                       @RequestParam Integer pageNo,
                                                                                       @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        CommonPage<HospitalOutpatient> commonPage = CommonPage.listPage(hospitalSpecialService.getOutPatientList(id));
        return CommonResult.success(commonPage);
    }
}
